import {
  createMemoryHistory,
  createRouter,
  createWebHashHistory
} from 'vue-router'

import HomeView from '../views/Home/index.vue'
import Show from '../views/showing/index.vue'
import Login from "../views/Login/index.vue";
import Register from "../views/Register/Register.vue";
import Active from "../views/active/index.vue";
import SelectActive from "../views/SelectActive/SelectActive.vue";
import Test from "../views/test/index.vue";
import Search from "../views/Search/Search.vue"
import Composition from "../views/composition/composition.vue"
import NewWord from "../views/NewWord/NewWord.vue"
import Review from "../views/review/review.vue"
import Forum from "../views/Forum/Forum.vue"
import LearningResources from "../views/LearningResources/LearningResources.vue"
import MemorizationSkills from "../views/MemorizationSkills/MemorizationSkills.vue";
import Mine from "../views/mine/mine.vue";
import Post from "../views/post/post.vue";
import SystemNotifications from "../views/SystemNotifications/SystemNotifications.vue"
import Chat from '../views/chat/chat.vue'
import ManageArticles from '../views/ManageArticles/ManageArticles.vue'
import ChangePassword from '../views/ChangePassword/ChangePassword.vue'

const routes = [{
  path: '/Home',
  component: HomeView,
  meta: {
    requiresAuth: true
  }
}, // 需要登录
{
  path: '/',
  component: Show
}, // 公开页面
{
  path: '/Login',
  component: Login
},
{
  path: '/Register',
  component: Register
},
{
  path: '/active',
  component: Active,
  meta: {
    requiresAuth: true
  }
}, // 需要登录
{
  path: '/SelectActive',
  component: SelectActive,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/Test',
  component: Test,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/Search',
  component: Search,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/Composition',
  component: Composition,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/NewWord',
  component: NewWord,
  meta: {
    requiresAuth: true
  }
}
  ,
{
  path: '/Review',
  component: Review,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/Forum',
  component: Forum,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/LearningResources',
  component: LearningResources,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/MemorizationSkills',
  component: MemorizationSkills,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/Mine',
  component: Mine,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/Post/:id',
  component: Post,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/SystemNotifications',
  component: SystemNotifications,
  meta: {
    requiresAuth: true
  }
},

{
  path: '/ManageArticles',
  component: ManageArticles,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/Chat',
  component: Chat,
  meta: {
    requiresAuth: true
  }
},
{
  path: '/ChangePassword',
  component: ChangePassword,
  meta: {
    requiresAuth: true
  }
}
];


const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 全局路由守卫，判断是否登录
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token'); // 获取 token
  console.log(isAuthenticated, 'isAuthenticated')

  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/Login'); // 未登录，跳转到登录页
  } else {
    next(); // 继续导航
  }
});




export default router